import os
import glob
import sox

# refactor by adding arguments

cwd = os.getcwd()

def sortKeyFunc(s):
    return int(os.path.basename(s)[:-4])

#refactoring

# def remix_audio(input_folder, output_folder):
#     input_filenames = sorted(glob.glob(cwd + '/' + input_folder + '*.wav'), key=sortKeyFunc)
#     output_directory = cwd + '/' + output_folder

#     def create_output_filename(input_filename, output_dir):
#         return os.path.join(output_dir, 'remix', os.path.split(input_filename)[1])

#     for input_file in input_filenames:
#         t = sox.Transformer()
#         # remix_dictionary = {1: [1], 2: [1]} 
#         remix_dictionary = {1: [2], 2: [2]}
#         t.remix(remix_dictionary)
#         out_name = create_output_filename(input_file, output_directory)
#         os.makedirs(os.path.split(out_name)[0], exist_ok=True)
#         t.build(input_file, out_name)

# def pad_audio(input_folder, output_folder, silence):
#     input_filenames = sorted(glob.glob(cwd + '/' + input_folder + '/remix/*.wav'), key=sortKeyFunc)
#     output_directory = cwd + '/' + output_folder

#     def create_output_filename(input_filename, output_dir):
#         return os.path.join(output_dir, 'padded', os.path.split(input_filename)[1])

#     for input_file in input_filenames:
#         t = sox.Transformer()
#         t.norm(-3)
#         t.fade(fade_in_len=0.2, fade_out_len=0.2)
#         t.pad(silence, 0)
#         out_name = create_output_filename(input_file, output_directory)
#         os.makedirs(os.path.split(out_name)[0], exist_ok=True)
#         t.build(input_file, out_name)

def remix_vocal():
    input_filenames = sorted(glob.glob(cwd + '/vocal/*.wav'), key=sortKeyFunc)
    output_directory = cwd + '/remix_vocal'

    def create_output_filename(input_filename, output_dir):
        return os.path.join(output_dir, 'remix', os.path.split(input_filename)[1])

    for input_file in input_filenames:
        t = sox.Transformer()
        # remix_dictionary = {1: [1], 2: [1]} 
        remix_dictionary = {1: [2], 2: [2]}
        t.remix(remix_dictionary)
        out_name = create_output_filename(input_file, output_directory)
        os.makedirs(os.path.split(out_name)[0], exist_ok=True)
        t.build(input_file, out_name)

def remix_field():
    input_filenames = sorted(glob.glob(cwd + '/field/*.wav'), key=sortKeyFunc)
    output_directory = cwd + '/remix_field'

    def create_output_filename(input_filename, output_dir):
        return os.path.join(output_dir, 'remix', os.path.split(input_filename)[1])

    for input_file in input_filenames:
        t = sox.Transformer()
        # remix_dictionary = {1: [1], 2: [1]} 
        remix_dictionary = {1: [2], 2: [2]}
        t.remix(remix_dictionary)
        out_name = create_output_filename(input_file, output_directory)
        os.makedirs(os.path.split(out_name)[0], exist_ok=True)
        t.build(input_file, out_name)

def pad_three_seconds():
    input_filenames = sorted(glob.glob(cwd + '/remix_vocal/remix/*.wav'), key=sortKeyFunc)
    output_directory = cwd + '/padded_three'

    def create_output_filename(input_filename, output_dir):
        return os.path.join(output_dir, 'padded', os.path.split(input_filename)[1])

    for input_file in input_filenames:
        t = sox.Transformer()
        t.norm(-3)
        t.fade(fade_in_len=0.2, fade_out_len=0.2)
        t.pad(1, 0)
        out_name = create_output_filename(input_file, output_directory)
        os.makedirs(os.path.split(out_name)[0], exist_ok=True)
        t.build(input_file, out_name)

def pad_one_second():
    input_filenames = sorted(glob.glob(cwd + '/remix_field/remix/*.wav'), key=sortKeyFunc)
    output_directory = cwd + '/padded_one'

    def create_output_filename(input_filename, output_dir):
        return os.path.join(output_dir, 'padded', os.path.split(input_filename)[1])

    for input_file in input_filenames:
        t = sox.Transformer()
        t.norm(-3)
        t.fade(fade_in_len=0.2, fade_out_len=0.2)
        t.pad(1, 0)
        out_name = create_output_filename(input_file, output_directory)
        os.makedirs(os.path.split(out_name)[0], exist_ok=True)
        t.build(input_file, out_name)

def combine_audio():
    audio_one = sorted(glob.glob(cwd + '/padded_one/padded/*.wav'), key=sortKeyFunc)
    audio_two = sorted(glob.glob(cwd + '/padded_three/padded/*.wav'), key=sortKeyFunc)
    paired_audio = [list(x) for x in zip(audio_one, audio_two)]
    cbn = sox.Combiner()
    for n in paired_audio:
        out_name = os.path.split(n[0])[1].replace('.wav', '') + '_and_' + 'vocal' + os.path.split(n[1])[1]
        cbn.build(n, cwd + '/audio_outputs/' + out_name, 'concatenate')


for afunc in (remix_vocal, remix_field, pad_three_seconds, pad_one_second, combine_audio):
    afunc()

# refactored function caller
# for afunc in (remix_audio(), remix_audio(), pad_audio(), pad_audio(), combine_audio):
#     afunc()
